package com.yt.base.controller.website;

import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.website.api.interfaces.NewsService;
import com.yt.website.api.interfaces.NewsService;
import com.yt.website.api.model.News;
import com.yt.website.api.model.News;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/web/news")
public class NewsController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private NewsService newsService;

	@RequestMapping("/list")
	public ModelAndView list(
			@RequestParam(value = "title", required = false) String title,
			@RequestParam(value = "start", required = false) String start,
			@RequestParam(value = "limit", required = false) String limit,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("website/news/newsList");
		int currentPage = 1;
		int pageSize = 25;
		if (StringUtils.isNotEmpty(limit) && StringUtils.isNumeric(limit)) {
			pageSize = Integer.parseInt(limit);
		}
		if (StringUtils.isNotEmpty(start) && StringUtils.isNumeric(start)) {
			currentPage = Integer.parseInt(start);
		}

		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.setPagingBean(new PagingBean((currentPage-1)*pageSize,pageSize));
		if (StringUtils.isNotEmpty(title)) {
			filter.addFilter("Q_title_S_LK", title.trim());
			mav.addObject("title", title);
		}
		filter.setSortField("id", true);
		List<News> userList = newsService.find(filter);
		int userCount = newsService.count(filter);
		mav.addObject("list", userList);
		mav.addObject("start", start);
		mav.addObject("limit", limit);
		mav.addObject("total", userCount);
		return mav;
	}

	@RequestMapping(value = "/toAdd")
	public ModelAndView toAdd(@RequestParam(value = "id", required = false) Long id) {
		ModelAndView mav = new ModelAndView("website/news/newsAdd");
		if (id != null && id > 0) {
			News news = newsService.get(id);
			if (news != null) {
				mav.addObject("bean", news);
			}
		}
		return mav;
	}

	@RequestMapping(value = "/save")
	public
	@ResponseBody
	Object add(@RequestParam(value = "id", required = false) Long id,
			   @RequestParam("title") String title,
			   @RequestParam(value = "newsType", required = false) Integer newsType,
			   @RequestParam(value = "sourceFrom") String sourceFrom,
			   @RequestParam(value = "content", required = false) String content,
			   @RequestParam(value = "author", required = false) String author,
			   HttpServletRequest request) {
		MsgBean msg = new MsgBean();
		try {
			News news = null;
			if (id != null && id > 0) {
				news = newsService.get(id);
			}
			if (news == null) {
				news = new News();
				news.setDateline(System.currentTimeMillis() / 1000);
			}
			news.setTitle(title.trim());
			news.setSourceFrom(sourceFrom);
			news.setType(newsType);
			news.setContent(content);
			news.setAuthor(author);
			if (news.getId() != null && news.getId() > 0) {
				newsService.update(news);
			} else {
				newsService.insert(news);
			}
		} catch (Exception e) {
			logger.error("添加或修改出错：{}", e.getMessage(), e);
			msg.setFailure(1, "添加失败！");
		}
		return msg.returnMsg();
	}

	@RequestMapping("/multiDel")
	@ResponseBody
	public Object multiDel(@RequestParam(value = "ids", required = true) String ids) {
		MsgBean msg = new MsgBean();
		try {
			String[] idArr = ids.split(",");
			for (int i = 0; i < idArr.length; i++) {
				newsService.remove(Long.parseLong(idArr[i]));
			}
		} catch (Exception ex) {
			logger.error("删除轮播图出错，", ex);
		}
		return msg.returnMsg();
	}
}
